import Vue from 'vue'
import Vuex from 'vuex'
//import { asyncRouterMap, constantRouterMap } from '@/router'
Vue.use(Vuex)

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.roles) {
//     return roles.some(role => route.meta.roles.includes(role))
//   } else {
//     return true
//   }
// }

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param routes asyncRouterMap
 * @param roles
 */
// function filterAsyncRouter(routes, roles) {
//   const res = []
//
//   routes.forEach(route => {
//     const tmp = { ...route }
//     if (hasPermission(roles, tmp)) {
//       if (tmp.children) {
//         tmp.children = filterAsyncRouter(tmp.children, roles)
//       }
//       res.push(tmp)
//     }
//   })
//
//   return res
// }


const store = new Vuex.Store({
  // 定义状态
  state: {
    userInfo: null, // 用户信息
    // rolePermission: [], // 权限列表
    // menuList: [], // 菜单列表
    // unreadNum: 0, // 未读消息数
    // roles: [], // 用户所有权限id （包括actionId）
    // routers: constantRouterMap,
    // addRouters: [],
  },
  getters: {
    getUserInfo: (state) => {
      state.userInfo = sessionStorage.getItem("userInfo");
      return JSON.parse(state.userInfo);
    }
    // getRolePermission: (state) => {
    //   state.rolePermission = sessionStorage.getItem("rolePermission");
    //   return JSON.parse(state.rolePermission);
    // },
    // getMenuList: (state) => {
    //   state.menuList = sessionStorage.getItem("menuList");
    //   return JSON.parse(state.menuList);
    // },
    // addRouters: (state) => {
    //   const { addRouters } = state
    //   sessionStorage.setItem("router", JSON.stringify(addRouters));
    //   return addRouters;
    // },
    // getRoles: (state) => {
    //   return state.roles;
    // }

  },
  mutations: {
    setUserInfo: (state, userInfo) => {
      state.userInfo = userInfo;
      sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
    }
    // ,
    // setRolePermission: (state, rolePermission) => {
    //   state.rolePermission = rolePermission;
    //   sessionStorage.setItem("rolePermission", JSON.stringify(rolePermission));
    // },
    // setMenuList: (state, menuList) => {
    //   state.menuList = menuList;
    //   sessionStorage.setItem("menuList", JSON.stringify(menuList));
    // },
    // setRouters: (state, routers) => {
    //   state.addRouters = routers;
    //   sessionStorage.setItem("routers", JSON.stringify(routers));
    //   state.routers = constantRouterMap.concat(routers);
    // },
    // setRoles: (state, roles) => {
    //   state.roles = roles;
    // }
  },
  actions: {
    setUserInfo: (context, userInfo) => context.commit("setUserInfo", userInfo)
  //   ,
  //   setRolePermission: (context, rolePermission) => context.commit("setRolePermission", rolePermission),
  //   setRoles: (context, roles) => context.commit("setRoles", roles),
  //   setMenuList: (context, menuList) => context.commit("setMenuList", menuList),
  //   GenerateRoutes(context, roles) {
  //     return new Promise(resolve => {
  //       let accessedRouters;
  //       accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
  //       context.commit('setRouters', accessedRouters)
  //       resolve();
  //     })
  //   }
  }
})

export default store
